package com.jushu.storbox.util;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.widget.Toast;

/**
 * 该工具用来管理相同内容的Toast的频繁程度
 * @author Tang
 *
 */
public class ToastFrequencyUtil {
	private Map<CharSequence, Long> timestampMap;
	private static final int TIME = 5000;
	private static ToastFrequencyUtil util;
	
	private ToastFrequencyUtil(){
		timestampMap = new HashMap<CharSequence, Long>();
	}
	
	public static ToastFrequencyUtil getInstence(){
		if (null == util) {
			util = new ToastFrequencyUtil();
		}
		return util;
	}
	
	public void showToast(Context context, CharSequence text, int duration){
		long currentMillis = System.currentTimeMillis();
		Long lastMillis = timestampMap.get(text);
		if (null == lastMillis || (currentMillis - lastMillis) > TIME) {
			Toast.makeText(context, text, duration).show();
			timestampMap.put(text, currentMillis);
		}
	}
	
	public void showToast(Context context, int resId, int duration){
		showToast(context, context.getResources().getText(resId), duration);
	}
	
	
}
